var COMPANY = {
		currentId : '0',
		loading: '',
		actionNameCandidate: '',
		actionNameJob: '',
		actionNameDelete: '',
		actionNameInterview: '',
		init: function(){
			$('tr.trdetail').hide();
			$('a.get-detail').click(function(){
				var $tr = $(this).parent().parent().next();
				var $divDetail = $('div.job-detail-information', $tr);
				var $divSeach = $('div.candidate-search', $tr);
				if(parseInt(COMPANY.currentId, 10) == parseInt($(this).attr('data-id'))){
					$tr.hide('fast');
					COMPANY.currentId = '0';
				}else{
					if(parseInt(COMPANY.currentId) != 0){
						$('tr.dtr_'+COMPANY.currentId).hide('fast');
					}
					COMPANY.currentId = $(this).attr('data-id');
					
					$divDetail.html(COMPANY.loading);
					$divSeach.html('').hide();
					$tr.show('fast');
					COMPANY.loadJobDetail($divDetail);
				}
			});
			$('a.ajax-search-candidate').click(function(){
				COMPANY.searchCandidate();
				return false;
			});
			$('a.ajax-load-interview').click(function(){
				COMPANY.loadInterview();
				return false;
			});
			
		},
		loadInterview: function(){
			var $detail = $('div.job-interview', $('tr.dtr_'+COMPANY.currentId));
			$detail.html(COMPANY.loading).show('fast');
			$.ajax({
				type: "POST",
				url: '/admin/companies/interview',
				data: {
					id: COMPANY.currentId,
					action_name: COMPANY.actionNameInterview,
				},
				cache: false,
				success: function(data){
					$detail.html(data);
					COMPANY.applyMaster();
				}
			});
		},
		loadJobDetail: function($detail){
			$.ajax({
				type: "POST",
				url: '/admin/companies/jobDetails',
				data: {
					id: COMPANY.currentId,
					action_name: COMPANY.actionNameJob
				},
				cache: false,
				success: function(data){
					$detail.html(data);
				}
			});
		},
		searchCandidate: function(){
			var $detail = $('div.candidate-search', $('tr.dtr_'+COMPANY.currentId));
			$detail.html(COMPANY.loading).show('fast');
			$.ajax({
				type: "POST",
				url: '/admin/candidates/searchCandidates',
				data: {
					job_id: COMPANY.currentId,
					action_name: COMPANY.actionNameCandidate
				},
				cache: false,
				success: function(data){
					$detail.html(data);
					COMPANY.applyInsert();
					COMPANY.candidateDetail();
				}
			});
		},
		applyInsert: function(){
			$('a.apply-insert').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html(COMPANY.loading);
				$.fancybox.open($str,{
					padding : 10,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: COMPANY.actionNameCandidate
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						COMPANY.applyForm();
					}
				});
			});
		},
		
		applyMaster: function(){
			$('a.apply-edit-in-job').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html(COMPANY.loading);
				$.fancybox.open($str,{
					padding : 10,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: COMPANY.actionNameCandidate
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						COMPANY.applyForm();
					}
				});
			});
			$('a.apply-delete-in-job').click(function(){
				var $ob = $(this);
				if(confirm($ob.attr('data-confirm'))){
					$.ajax({
						type: "POST",
						url: '/admin/candidates/apply',
						data: {
							id: $ob.attr('data-id'),
							action_name: COMPANY.actionNameDelete
						},
						cache: false,
						success: function(data){
							var js = $.parseJSON(data);
							alert(js.data);
							if(parseInt(js.status) == 1){
								$ob.parent().parent().remove();
							}
						}
					});
				}
			});
		},
		
		applyForm: function(){
			$('input.date-picker-interview').datepicker({minDate: $('#date_apply').val()});
			$('span.error').hide();
			$('input#button-submit').click(function(){
				$('span.error').hide();
				var $ob = $(this);
				var $dateInterview = $('#CandidateToJobs_date_interview');
				var $note = $('#CandidateToJobs_note_description');
				var $result =$('select#CandidateToJobs_result_status');// $('input[type=radio].result:checked');//children('option:selected').text()
				var $comment = $('#CandidateToJobs_comment_from_employer');
				var $candidate = $('#CandidateToJobs_candidate_id');
				var $job = $('#CandidateToJobs_job_id');
				
				if($dateInterview.val()==''){
					$dateInterview.next('span.error').show();
					$dateInterview.focus();
					$dateInterview.focus();
					return false;
				}
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $candidate.val(),
						job_id: $job.val(),
						note_description: $note.val(),
						date_interview: $dateInterview.val(),
						comment_from_employer:  $comment.val(),
						result_status: $result.children('option:selected').val(),
						
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						var js = $.parseJSON(data);
						alert(js.data);
						if(parseInt(js.status)==1){
							$.fancybox.close();
							var $tr = $('tr.dtr_'+COMPANY.currentId);
							var $divDetail = $('div.job-detail-information', $tr);
							var $divSeach = $('div.candidate-search', $tr);
							$divDetail.html(COMPANY.loading);
							$divSeach.html('').hide();
							COMPANY.loadJobDetail($divDetail);
						}
					}
				});
			});
			
		},
		candidateDetail: function (){
			$('a.candidate-detail').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				$str.html(COMPANY.loading);
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				
				$.ajax({
					type: "POST",
					url: '/admin/candidates/candidateDetails',
					data: {
						id: $ob.attr('data-id'),
						action_name: COMPANY.actionNameCandidate
					},
					cache: false,
					success: function(data){
						var strControl = '<div class="dt-control">'
							+'<a href="#" class="apply-insert" data-title="'+$ob.attr('data-apply')+'" data-job-id="'+COMPANY.currentId+'" data-candidate-id="'+$ob.attr('data-id')+'">'
							+$ob.attr('data-apply')
							+'</a>';
							+'</div>';
						$.fancybox.close();
						$.fancybox.open($str.html(strControl).append(data).append(strControl),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						COMPANY.applyJobDetail();
						
					}
				});
				
				
			});
		},
		applyJobDetail: function(){
			$('a.apply-edit, a.apply-insert').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html(COMPANY.loading);
				$.fancybox.open($str,{
					padding : 10,
					title: $ob.attr('data-title'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: COMPANY.actionNameCandidate
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						COMPANY.applyForm();
					}
				});
			});
			$('a.apply-delete').click(function(){
				var $ob = $(this);
				if(confirm($ob.attr('data-confirm'))){
					$.ajax({
						type: "POST",
						url: '/admin/candidates/apply',
						data: {
							id: $ob.attr('data-id'),
							action_name: COMPANY.actionNameDelete
						},
						cache: false,
						success: function(data){
							var js = $.parseJSON(data);
							alert(js.data);
							if(parseInt(js.status) == 1){
								$ob.parent().parent().remove();
							}
						}
					});
				}
			});
		},
		
		//FORM
		addChildCreate: function(){
			$('a.add-child').click(function(){
				var $oName = $('input#input_name');
				var $oPosition = $('select#input_position');
				var $oEmail = $('input#input_email');
				var $oPhone = $('input#input_phone');
				if($oName.val()==''){
					alert($oName.attr('data-error'));
					$oName.focus();
					$oName.focus();
					return false;
				};
				if($oPosition.val()=='-1'){
					alert($oPosition.attr('data-error'));
					$oPosition.focus();
					$oPosition.focus();
					return false;
				}
				if($oEmail.val()!='' && !VALIDATOR.isEmail($oEmail.val())){
					alert($oEmail.attr('data-error'));
					$oEmail.focus();
					$oEmail.focus();
					return false;
				}
				
				var addInput = $('<input name="personContacts[]" type="hidden" value=""/>');
				addInput.val($oName.val()+';'+$oPosition.children('option:selected').val()+';'+$oEmail.val()+';'+$oPhone.val());
				
				var controlTd = $('<td class="child-control"></td>')
					.append('<a href="#" class="remove-child" title="">&nbsp;</a>')
					.append(addInput);
				var element = $('<tr></tr>')
						.append('<td class="child">'+$oName.val()+'</td>')
						.append('<td class="child">'+$oPosition.children('option:selected').text()+'</td>')
						.append('<td class="child">'+$oEmail.val()+'</td>')
						.append('<td class="child">'+$oPhone.val()+'</td>')
						.append(controlTd);
				$(this).parent().parent().before(element);
				$oName.val('');
				$oPosition.val('-1');
				$oEmail.val('');
				$oPhone.val('');
				COMPANY.removeChild();
				return false;
			});
			COMPANY.removeChild();
			return false;
		},
		removeChild: function(){
			$('a.remove-child').click(function(){
				$(this).parent().parent().remove();
			});
		}
}